import { BASE_URL } from '/utils/CommonValues.js';
import { loginSystem } from '/api/login.js';

export function GET(url, data) {
	return SIMPLE(url, data, 'GET');
}

export function POST(url, data) {
	return SIMPLE(url, data, 'POST');
}

export function DELETE(url, data) {
	return SIMPLE(url, data, 'DELETE');
}

export function PUT(url, data) {
	return SIMPLE(url, data, 'PUT');
}

export function SIMPLE(url, data, method) {
	const token = uni.getStorageSync("APP_TOKEN");
	// console.log("token为:", token)
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${BASE_URL}${url}`,
			method: method,
			header: {
				"Content-Type": "application/json;charset=utf-8",
				"Authorization": token
			},
			dataType: 'json',
			data: data,
			success: res => {
				// console.log(res)
				if(res.data.code == '200' || res.data.statusCode == '200') {
					resolve(res.data)
				} else {
					reject(res)
				}
			},
			fail: err => {
				reject(err)
			}
		})
	})
}

export function SIMPLE1(url, data, method) {
	return new Promise((resolve, reject) => {
		loginSystem().then(token => {
			uni.request({
				url: `${BASE_URL}${url}`,
				method: method,
				header: {
					"Content-Type": "application/json;charset=utf-8",
					"Authorization": token
				},
				dataType: 'json',
				data: data,
				success: res => {
					// console.log(res)
					if(res.data.code == '200' || res.data.statusCode == '200') {
						resolve(res.data)
					} else {
						reject(res)
					}
				},
				fail: err => {
					reject(err)
				}
			})
		}).catch(error => {
			console.log(error)
			uni.showToast({
				title: '登录信息错误',
				icon: 'none'
			})
		}) 
	})
}

export default {
	GET,
	POST,
	DELETE,
	PUT
}